package com.tianfei.crowd.service.impl;

import com.tianfei.crowd.entity.po.MemberPO;
import com.tianfei.crowd.entity.po.MemberPOExample;
import com.tianfei.crowd.mapper.MemberPOMapper;
import com.tianfei.crowd.service.api.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * MySQL-Provider 端 本地的 ServiceImpl
 *
 *
 * @author: Herz
 * @date: 2021/7/27 20:45
 */
@Transactional(readOnly = true)
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberPOMapper memberPOMapper;

    @Override
    public MemberPO getMemberPOByLoginAcct(String loginAcct) {

        // 1、创建一个 MemberPOExample 对象
        MemberPOExample example = new MemberPOExample();

        // 2、创建一个 Criteria 对象
        MemberPOExample.Criteria criteria = example.createCriteria();

        // 3、添加查询条件
        criteria.andLoginacctEqualTo(loginAcct);

        // 4、执行查询
        List<MemberPO> list = memberPOMapper.selectByExample(example);

        if (list == null || list.size() == 0){
            return null;
        }

        // 5、获取结果
        return list.get(0);
    }

    @Transactional(
            propagation = Propagation.REQUIRES_NEW,
            rollbackFor = Exception.class,
            readOnly = false)
    @Override
    public void saveMember(MemberPO memberPO) {
        memberPOMapper.insertSelective(memberPO);
    }
}
